package com.qq.googleplay.ui.fragment;

import android.graphics.Color;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;

import com.haarman.listviewanimations.swinginadapters.AnimationAdapter;
import com.haarman.listviewanimations.swinginadapters.prepared.ScaleInAnimationAdapter;
import com.qq.googleplay.base.BaseFragment;
import com.qq.googleplay.base.BaseHolder;
import com.qq.googleplay.base.LoadingPager;
import com.qq.googleplay.base.SuperBaseAdapter;
import com.qq.googleplay.bean.AppInfoBean;
import com.qq.googleplay.factory.ListViewFactory;
import com.qq.googleplay.http.Protocol.GameProtocol;
import com.qq.googleplay.ui.holder.GameItemHolder;
import com.qq.googleplay.utils.UIUtils;

import java.util.List;

/**
 * ============================================================
 * 版 权 ： Google互联网有限公司版权所有 (c) 2016
 * 作 者 : 陈冠杰
 * 版 本 ： 1.0
 * 创建日期 ：2016/3/20 19:37
 * 描 述 ：游戏
 * 修订历史 ：
 * ============================================================
 **/
public class GameFragment extends BaseFragment {


    private GameProtocol mGameProtocol;
    private List<AppInfoBean> mData;

    private  GameAdapter mGameAdapter ;

    @Override
    protected View initSuccessView() {
        ListView listView = ListViewFactory.createListView();
        listView.setBackgroundColor(Color.parseColor("#eaeaea"));
        listView.setPadding(0, UIUtils.dip2Px(5),0,0);
        mGameAdapter = new GameAdapter(listView, mData) ;
        //给listview的item添加缩放动画
        AnimationAdapter animAdapter = new ScaleInAnimationAdapter(mGameAdapter);
        animAdapter.setAbsListView(listView);
        listView.setAdapter(animAdapter);
        //listView.setAdapter();
        return listView;
    }

    @Override
    protected LoadingPager.LoadedResult initData() {
        mGameProtocol = new GameProtocol();
        try {
            mData = mGameProtocol.loadData(0);
            return checkState(mData);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return LoadingPager.LoadedResult.ERROR;
        }
    }

    class GameAdapter extends SuperBaseAdapter<AppInfoBean> {
        public GameAdapter(AbsListView listView, List<AppInfoBean> dataSource) {
            super(listView, dataSource);
        }

        @Override
        public BaseHolder<AppInfoBean> getSpecialHolder(int position) {
            return new GameItemHolder(mActivity);
        }

        @Override
        public List<AppInfoBean> onLoadMore() throws Throwable {

            return mGameProtocol.loadData(mData.size());
        }
    }
}
